//
//  DLDefine.h
//  多选相册照片
//
//  Created by long on 15/11/26.
//  Copyright © 2015年 long. All rights reserved.
//

#ifndef DLDefine_h
#define DLDefine_h

#import "DLProgressHUD.h"
#import "NSBundle+DLPhotoBrowser.h"

#define DLPhotoBrowserCameraText @"DLPhotoBrowserCameraText"
#define DLPhotoBrowserCameraRecordText @"DLPhotoBrowserCameraRecordText"
#define DLPhotoBrowserAblumText @"DLPhotoBrowserAblumText"
#define DLPhotoBrowserCancelText @"DLPhotoBrowserCancelText"
#define DLPhotoBrowserOriginalText @"DLPhotoBrowserOriginalText"
#define DLPhotoBrowserDoneText @"DLPhotoBrowserDoneText"
#define DLPhotoBrowserOKText @"DLPhotoBrowserOKText"
#define DLPhotoBrowserBackText @"DLPhotoBrowserBackText"
#define DLPhotoBrowserPhotoText @"DLPhotoBrowserPhotoText"
#define DLPhotoBrowserPreviewText @"DLPhotoBrowserPreviewText"
#define DLPhotoBrowserLoadingText @"DLPhotoBrowserLoadingText"
#define DLPhotoBrowserHandleText @"DLPhotoBrowserHandleText"
#define DLPhotoBrowserSaveImageErrorText @"DLPhotoBrowserSaveImageErrorText"
#define DLPhotoBrowserMaxSelectCountText @"DLPhotoBrowserMaxSelectCountText"
#define DLPhotoBrowserNoCameraAuthorityText @"DLPhotoBrowserNoCameraAuthorityText"
#define DLPhotoBrowserNoAblumAuthorityText @"DLPhotoBrowserNoAblumAuthorityText"
#define DLPhotoBrowserNoMicrophoneAuthorityText @"DLPhotoBrowserNoMicrophoneAuthorityText"
#define DLPhotoBrowseriCloudPhotoText @"DLPhotoBrowseriCloudPhotoText"
#define DLPhotoBrowserGifPreviewText @"DLPhotoBrowserGifPreviewText"
#define DLPhotoBrowserVideoPreviewText @"DLPhotoBrowserVideoPreviewText"
#define DLPhotoBrowserLivePhotoPreviewText @"DLPhotoBrowserLivePhotoPreviewText"
#define DLPhotoBrowserNoPhotoText @"DLPhotoBrowserNoPhotoText"
#define DLPhotoBrowserCannotSelectVideo @"DLPhotoBrowserCannotSelectVideo"
#define DLPhotoBrowserCannotSelectGIF @"DLPhotoBrowserCannotSelectGIF"
#define DLPhotoBrowserCannotSelectLivePhoto @"DLPhotoBrowserCannotSelectLivePhoto"
#define DLPhotoBrowseriCloudVideoText @"DLPhotoBrowseriCloudVideoText"
#define DLPhotoBrowserEditText @"DLPhotoBrowserEditText"
#define DLPhotoBrowserSaveText @"DLPhotoBrowserSaveText"
#define DLPhotoBrowserMaxVideoDurationText @"DLPhotoBrowserMaxVideoDurationText"
#define DLPhotoBrowserLoadNetImageFailed @"DLPhotoBrowserLoadNetImageFailed"
#define DLPhotoBrowserSaveVideoFailed @"DLPhotoBrowserSaveVideoFailed"
#define DLPhotoBrowserRequestTimeout @"DLPhotoBrowserRequestTimeout"

#define DLPhotoBrowserCameraRoll @"DLPhotoBrowserCameraRoll"
#define DLPhotoBrowserPanoramas @"DLPhotoBrowserPanoramas"
#define DLPhotoBrowserVideos @"DLPhotoBrowserVideos"
#define DLPhotoBrowserFavorites @"DLPhotoBrowserFavorites"
#define DLPhotoBrowserTimelapses @"DLPhotoBrowserTimelapses"
#define DLPhotoBrowserRecentlyAdded @"DLPhotoBrowserRecentlyAdded"
#define DLPhotoBrowserBursts @"DLPhotoBrowserBursts"
#define DLPhotoBrowserSlomoVideos @"DLPhotoBrowserSlomoVideos"
#define DLPhotoBrowserSelfPortraits @"DLPhotoBrowserSelfPortraits"
#define DLPhotoBrowserScreenshots @"DLPhotoBrowserScreenshots"
#define DLPhotoBrowserDepthEffect @"DLPhotoBrowserDepthEffect"
#define DLPhotoBrowserLivePhotos @"DLPhotoBrowserLivePhotos"
#define DLPhotoBrowserAnimated @"DLPhotoBrowserAnimated"
#define DLPhotoBrowserMaxVideoSelectCountInMix @"DLPhotoBrowserMaxVideoSelectCountInMix"
#define DLPhotoBrowserMinVideoSelectCountInMix @"DLPhotoBrowserMinVideoSelectCountInMix"


#if DEBUG
#define DLLoggerDebug(format, ...) NSLog(format, ##__VA_ARGS__)
#else
#define DLLoggerDebug(format, ...)
#endif

#define kRGB(r, g, b)   [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]


#ifndef DL_weakify
    #if DEBUG
        #if __has_feature(objc_arc)
        #define DL_weakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;
        #else
        #define DL_weakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;
        #endif
    #else
        #if __has_feature(objc_arc)
        #define DL_weakify(object) try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;
        #else
        #define DL_weakify(object) try{} @finally{} {} __block __typeof__(object) block##_##object = object;
        #endif
    #endif
#endif

#ifndef DL_strongify
    #if DEBUG
        #if __has_feature(objc_arc)
        #define DL_strongify(object) autoreleasepool{} __typeof__(object) object = weak##_##object;
        #else
        #define DL_strongify(object) autoreleasepool{} __typeof__(object) object = block##_##object;
        #endif
    #else
        #if __has_feature(objc_arc)
        #define DL_strongify(object) try{} @finally{} __typeof__(object) object = weak##_##object;
        #else
        #define DL_strongify(object) try{} @finally{} __typeof__(object) object = block##_##object;
        #endif
    #endif
#endif


//static NSString * const kDLBundleName = @"DLPhotoBrowser.bundle";//资源包名称
//#define kDLPhotoBrowserXIBName(_name_) [NSString stringWithFormat:@"%@/%@",kDLBundleName,_name_]
#define kDLPhotoBrowserBundle [NSBundle bundleForClass:[self class]]
//#define kDLPhotoBrowserBundle [[NSBundle mainBundle] pathForResource:@"DLPhotoBrowser" ofType:@"bundle"]

// 图片路径
//#define kDLPhotoBrowserSrcName(file) [NSString stringWithFormat:@"%@/DLPhotoBrowser.bundle/%@",kDLBundleName,file]
#define kDLPhotoBrowserSrcName(file) [@"DLPhotoBrowser.bundle/" stringByAppendingPathComponent:file]
#define kDLPhotoBrowserFrameworkSrcName(file) [@"Frameworks/DLPhotoBrowser.framework/DLPhotoBrowser.bundle" stringByAppendingPathComponent:file]

#define kDLViewWidth      [[UIScreen mainScreen] bounds].size.width
#define kDLViewHeight     [[UIScreen mainScreen] bounds].size.height

//app名字
#define kDL_LOCALIZED_APP_NAME [[NSBundle mainBundle].localizedInfoDictionary objectForKey:@"CFBundleDisplayName"]
#define kDLAPPName kDL_LOCALIZED_APP_NAME ?: [[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleDisplayName"] ?: [[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleName"]

//自定义图片名称存于plist中的key
#define DLCustomImageNames @"DLCustomImageNames"
//设置框架语言的key
#define DLLanguageTypeKey @"DLLanguageTypeKey"
//自定义多语言key value存于plist中的key
#define DLCustomLanguageKeyValue @"DLCustomLanguageKeyValue"

////////DLShowBigImgViewController
#define kDLItemMargin 40

///////DLBigImageCell 不建议设置太大，太大的话会导致图片加载过慢
#define kDLMaxImageWidth 500

#define DLClippingRatioValue1 @"value1"
#define DLClippingRatioValue2 @"value2"
#define DLClippingRatioTitleFormat @"titleFormat"

#define DLPreviewPhotoObj @"DLPreviewPhotoObj"
#define DLPreviewPhotoTyp @"DLPreviewPhotoTyp"

typedef NS_ENUM(NSUInteger, DLLanguageType) {
    //跟随系统语言，默认
    DLLanguageSystem,
    //中文简体
    DLLanguageChineseSimplified,
    //中文繁体
    DLLanguageChineseTraditional,
    //英文
    DLLanguageEnglish,
    //日文
    DLLanguageJapanese,
};

//录制视频及拍照分辨率
typedef NS_ENUM(NSUInteger, DLCaptureSessionPreset) {
    DLCaptureSessionPreset325x288,
    DLCaptureSessionPreset640x480,
    DLCaptureSessionPreset1280x720,
    DLCaptureSessionPreset1920x1080,
    DLCaptureSessionPreset3840x2160,
};

//导出视频类型
typedef NS_ENUM(NSUInteger, DLExportVideoType) {
    //default
    DLExportVideoTypeMov,
    DLExportVideoTypeMp4,
};

//导出视频水印位置
typedef NS_ENUM(NSUInteger, DLWatermarkLocation) {
    DLWatermarkLocationTopLeft,
    DLWatermarkLocationTopRight,
    DLWatermarkLocationCenter,
    DLWatermarkLocationBottomLeft,
    DLWatermarkLocationBottomRight,
};

//混合预览图片时，图片类型
typedef NS_ENUM(NSUInteger, DLPreviewPhotoType) {
    DLPreviewPhotoTypePHAsset,
    DLPreviewPhotoTypeUIImage,
    DLPreviewPhotoTypeURLImage,
    DLPreviewPhotoTypeURLVideo,
};

static inline BOOL DL_DeviceIsiPhone() {
    return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone;
}

static inline CGFloat DL_SafeAreaBottom() {
    CGFloat temp = 0;
    
    if (@available(iOS 11.0, *)) {
        temp = UIApplication.sharedApplication.keyWindow.safeAreaInsets.bottom;
    }
    
    return temp;
}

static inline NSDictionary * GetDictForPreviewPhoto(id obj, DLPreviewPhotoType type) {
    if (nil == obj) {
        @throw [NSException exceptionWithName:@"error" reason:@"预览对象不能为空" userInfo:nil];
    }
    return @{DLPreviewPhotoObj: obj, DLPreviewPhotoTyp: @(type)};
}

static inline void SetViewWidth(UIView *view, CGFloat width) {
    CGRect frame = view.frame;
    frame.size.width = width;
    view.frame = frame;
}

static inline CGFloat GetViewWidth(UIView *view) {
    return view.frame.size.width;
}

static inline void SetViewHeight(UIView *view, CGFloat height) {
    CGRect frame = view.frame;
    frame.size.height = height;
    view.frame = frame;
}

static inline CGFloat GetViewHeight(UIView *view) {
    return view.frame.size.height;
}

static inline NSString *  GetLocalLanguageTextValue (NSString *key) {
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] valueForKey:DLCustomLanguageKeyValue];
    if ([dic.allKeys containsObject:key]) {
        return dic[key];
    }
    return [NSBundle DLLocalizedStringForKey:key];
}

static inline UIImage * GetImageWithName(NSString *name) {
    NSArray *names = [[NSUserDefaults standardUserDefaults] valueForKey:DLCustomImageNames];
    if ([names containsObject:name]) {
        return [UIImage imageNamed:name];
    }
    return [UIImage imageNamed:kDLPhotoBrowserSrcName(name)]?:[UIImage imageNamed:kDLPhotoBrowserFrameworkSrcName(name)];
}

static inline CGFloat GetMatchValue(NSString *text, CGFloat fontSize, BOOL isHeightFixed, CGFloat fixedValue) {
    CGSize size;
    if (isHeightFixed) {
        size = CGSizeMake(MAXFLOAT, fixedValue);
    } else {
        size = CGSizeMake(fixedValue, MAXFLOAT);
    }
    
    CGSize resultSize;
    if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 7.0) {
        //返回计算出的size
        resultSize = [text boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size;
    }
    if (isHeightFixed) {
        return resultSize.width;
    } else {
        return resultSize.height;
    }
}

static inline void ShowAlert(NSString *message, UIViewController *sender) {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:GetLocalLanguageTextValue(DLPhotoBrowserOKText) style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:action];
    [sender presentViewController:alert animated:YES completion:nil];
}

static inline CABasicAnimation * GetPositionAnimation(id fromValue, id toValue, CFTimeInterval duration, NSString *keyPath) {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:keyPath];
    animation.fromValue = fromValue;
    animation.toValue   = toValue;
    animation.duration = duration;
    animation.repeatCount = 0;
    animation.autoreverses = NO;
    //以下两个设置，保证了动画结束后，layer不会回到初始位置
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    return animation;
}

static inline CAKeyframeAnimation * GetBtnStatusChangedAnimation() {
    CAKeyframeAnimation *animate = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    
    animate.duration = 0.3;
    animate.removedOnCompletion = YES;
    animate.fillMode = kCAFillModeForwards;
    
    animate.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.7, 0.7, 1.0)],
                       [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)],
                       [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)],
                       [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    return animate;
}

static inline NSInteger GetDuration (NSString *duration) {
    NSArray *arr = [duration componentsSeparatedByString:@":"];
    
    NSInteger d = 0;
    for (int i = 0; i < arr.count; i++) {
        d += [arr[i] integerValue] * pow(60, (arr.count-1-i));
    }
    return d;
}


static inline NSDictionary * GetCustomClipRatio() {
    return @{DLClippingRatioValue1: @(0), DLClippingRatioValue2: @(0), DLClippingRatioTitleFormat: @"Custom"};
}

static inline NSDictionary * GetClipRatio(NSInteger value1, NSInteger value2) {
    return @{DLClippingRatioValue1: @(value1), DLClippingRatioValue2: @(value2), DLClippingRatioTitleFormat: @"%g : %g"};
}

#endif /* DLDefine_h */
